package org.meow.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.meow.dto.NewsDTO;
import org.meow.entity.News;
import org.meow.result.Result;
import org.meow.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * &#064;Date: 24/04/22-19:02
 * &#064;Description: 新闻
 */
@RestController
@RequestMapping("/news")
@Tag(name = "新闻")
public class NewsController {

  @Autowired
  private NewsService newsService;

  @PostMapping
  @Operation(summary = "添加新闻")
  public Result<String> addNews(@RequestBody NewsDTO newsDTO) {
    newsService.save(newsDTO);
    return Result.success();
  }

  @PutMapping("/{id}")
  @Operation(summary = "修改新闻")
  public Result<String> modifyNews(@PathVariable Integer id,@RequestBody NewsDTO news) {
    return newsService.updateNews(id, news) ? Result.success() : Result.error("修改失败");
  }

  @DeleteMapping("/{id}")
  @Operation(summary = "删除新闻")
  public Result<String> deleteNews(@PathVariable int id) {
    return newsService.deleteNews(id) ? Result.success() : Result.error("删除失败");
  }
}
